<template>
	<view class="container">
		<view class="topbox">
			<image src="/static/login/logo.png" style="width: 300rpx;height: 300rpx;" mode=""></image>
			<text class="login_txt">七星校园</text>
		</view>
		
		
		<button class="weicat_login" open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber">微信用户一键登录</button>
		<view class="phone-txt" @click="tophone">
			手机号登录注册
		</view>
		
		<view class="xieyi">
			<image style="width: 28rpx;height: 28rpx;" v-if="isShow" @click="isShow = !isShow"
				src="../../static/login/yes.png" mode=""></image>
			<image style="width: 28rpx;height: 28rpx;" v-else @click="isShow = !isShow"
				src="../../static/login/no.png" mode="">
			</image>
			登录注册即视为你同意<text @click="navto1(1,'服务条款')">《服务条款》</text>和<text @click="navto1(2,'隐私条款')">《隐私条款》</text>
		</view>
	</view>
</template>
<script>
	var app = getApp( )
	import ikz from '@/pages/Common/tools.js'
	export default {
		data( ) {
			return {
				isShow:false,
				isRegister:null
			}
		},
		onLoad( ) {

		},
		methods: {
			navto1(type,title){
				if(type == 1){
					uni.navigateTo( {
						url: '/pages/Component/Module/Other/Program/agreement/index'
					} )
				}else{
					uni.navigateTo( {
						url: '/pages/Component/Module/Other/Program/insiinfo/insiinfo'
					} )
				}
			
			},
			// 获取账号是否已注册
			getAuthStatus( fn ) {
				var _this = this
				uni.login( {
					provider: 'weixin',
					success: loginRes => {
						var path = '/xyb/user/is_register';
						var data = {
							code: loginRes.code
						};
						ikz.request( path, data, res => {
							console.log(res,'erere');
							_this.isRegister = res.data.data.auth_status 
							if ( res.data.code == 1 ) {
								_this.loginEvent( ) 
							} else {
								uni.showModal( {
									title: '提示',
									content: '服务器繁忙，请稍后再试',
									showCancel: false,
									success: function( res ) {}
								} );
							}

						} );
					},
					fail: ( ) => {
						uni.showToast( {
							icon: "none",
							title: "授权失败，请重试"
						} )
					}
				} )
			},
			// 返回上一页
			goBack( ) {
				uni.navigateTo( {
					url: "/pages/login/setxiaoqu/setxiaoqu"
				} )
			},
			tophone(){
				uni.navigateTo({
					url:'/pages/login/phonelogin/phonelogin'
				})
			},
			// 获取用户手机号码
			decryptPhoneNumber( e ) {
				console.log( e, 'eeee' );
				
				if(!this.isShow) return uni.showToast({
					title:'请先同意隐私协议',
					icon:'none'
				})
				if ( e.detail.errMsg == "getPhoneNumber:ok" ) {
					uni.showLoading( {
						title: "加载中",
						mask: true
					} )
					wx.requirePrivacyAuthorize( {
						success: ( ) => {
							uni.login( {
								provider: 'weixin',
								success: loginRes => {
									let data = e.detail
									data.code = loginRes.code
									var path = '/xyb/user/get_phone';
									ikz.request( path, data, res => {
										if ( res.data.code == 1 ) {
											uni.hideLoading( )
											this.phoneNumber = res.data.data.phoneNumber
											getApp( ).globalData.mobile = res.data.data
												.phoneNumber
												
												this.getAuthStatus( res.data.data.phoneNumber )
												
										} else {
											uni.showToast( {
												title: res.data.msg,
												icon: 'none'
											} )
										}

									} );
								},
								fail: ( ) => {
									uni.hideLoading( )
									uni.showToast( {
										icon: "none",
										title: "授权失败，请重试"
									} )
								}
							} );
						}
					} )
					fail: ( ) => {
						uni.hideLoading( )
						uni.showToast( {
							title: "需要同意隐私协议才能使用",
							icon: 'none'
						} )
					}


				} else {
					uni.showToast( {
						title: '获取失败，请重新获取',
						icon: 'none'
					} )
				}
			},
			// 选择头像  
			bindChooseAvatar( e ) {
				this.avatarPath = e.detail.avatarUrl
			},
			// 提交登录
			handleLogin( e ) {
				uni.showLoading( {
					title: '授权中',
					mask: true,
				} )
				if ( this.avatarPath ) {
					uni.uploadFile( {
						url: app.globalData.adminPath + '/xyb/common/upload',
						filePath: this.avatarPath,
						name: 'file',
						formData: {},
						success: uploadFileRes => {
							this.userInfo.avatar = JSON.parse( uploadFileRes.data ).data.url
							this.loginEvent( )
						},
						complete: ( ) => {
							uni.hideLoading( )
						},
					} )
				} else {
					this.loginEvent( )
				}
			},
			// 提交事件
			loginEvent( ) {
				var _this = this
				uni.login( {
					provider: 'weixin',
					success: loginRes => {
						let path = ""
						let data = {
							code: loginRes.code
						}
						if ( _this.isRegister ) {
							path = "/xyb/user/login"
						} else {
							path = "/xyb/user/register"
							data.mobile = _this.phoneNumber
							if ( uni.getStorageSync( "parentWechatId" ) ) data.parent_wechat_id = uni
								.getStorageSync( "parentWechatId" )
						}
						ikz.requestPost( path, data, res => {
							console.log(res,'sssss');
							if ( res.data.code == 1 ) {
								uni.setStorageSync( 'local_user_session', res.data.data.token );
								let user_info = {
									mobile: res.data.data.mobile,
									agent_area: res.data.data.agent_area,
									nickname: res.data.data.nickname,
									avatar: res.data.data.avatar,
								}
								uni.setStorageSync( "user_info", user_info )
								_this.goBack( )
							} else {
								uni.showToast( {
									title: res.data.msg,
									icon: 'none'
								} )
							}

						} );
					},
					fail: ( ) => {
						uni.showToast( {
							icon: "none",
							title: "授权失败，请重试"
						} )
					}
				} )
			},
			// 稍后授权
			laterAuthorize( ) {
				uni.showLoading( {
					title: '授权中',
					mask: true,
				} )
				this.loginEvent( )
			},
		}
	}
</script>
<style lang="scss">
	page {
		background: #fff;
	}

	.container {
		padding: 96rpx 0;
		box-sizing: border-box;


		.topbox {
			width: 100%;
			height: 542rpx;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;


			.login_txt {
				width: 128rpx;
				height: 48rpx;
				font-weight: 500;
				font-size: 32rpx;
				color: #000000;
				text-align: center;
				margin-top: 30rpx;
			}
		}

		.weicat_login {
			width: 690rpx;
			height: 100rpx;
			background: #247EFF;
			border-radius: 16rpx 16rpx 16rpx 16rpx;
			text-align: center;
			line-height: 100rpx;
			margin: 32rpx auto;
			font-weight: 400;
			font-size: 30rpx;
			color: #FFFFFF;
			text-align: center;
		}

		.phone-txt {
			width: 690rpx;
			height: 100rpx;
			background: #FFFFFF;
			border-radius: 16rpx 16rpx 16rpx 16rpx;
			border: 2rpx solid #247EFF;
			line-height: 100rpx;
			margin: 0 auto;
			font-weight: 400;
			font-size: 30rpx;
			color: #247EFF;
			text-align: center;
		}
		
		.xieyi {
			height: 36rpx;
			font-size: 22rpx;
			font-weight: 500;
			color: #000000;
			margin: 32rpx 44rpx;
			// text-align: center;
		
			image {
				width: 33rpx;
				height: 33rpx;
				margin-right: 15rpx;
				line-height: 36rpx;
			}
		
			text {
				color: #247EFF;
			}
		}
		
	}
</style>